var Define = require("../../../lib/Define.js")

var _activityId = 0
var _userInfo = {}
var _phone = ""
Page({
  data:{
    
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var self = this

    _activityId = options.activityId

    Define.getUserInfo(function(userInfo){
      _userInfo = userInfo
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  inputPhoneAction:function(e){
    _phone = e.detail.value
  },
  signAction:function(){
    var self = this

    if(!_phone.length){
      wx.showToast({
        title: '请输入手机号',
        icon: 'loading',
        duration: 2000
      })
      return
    }

    Define.getUserInfo(function(userInfo){

      if(userInfo){
        wx.request({
          url: Define.value.baseUrl+'/app/activity/sign',
          data: {
            bid:Define.value.bid,
            userId:userInfo.id,
            activityId:_activityId,
            phone:userInfo.phone
          },
          method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          // header: {}, // 设置请求的 header
          success: function(res){
            // success
            if(res.data.code == 200){
              Define.updateUserPhone(_phone)
            }

            wx.showModal({
              title: "报名成功",
              content: '是否联系小蜜？',
              success: function(res) {
                if (res.confirm) {
                  
                  wx.navigateTo({
                    url: '../../sendmsg/sendmsg'
                  })

                } else if (res.cancel) {
                  wx.navigateBack({
                    delta: 1
                  })
                }
              }
            })
            
          },
          fail: function(res) {
            // fail
          },
          complete: function(res) {
            // complete
          }
        })
        
      }

    })
    
  }
})